module sobel_edge_detect;
reg signed [7:0] pix_0;
reg signed [7:0] pix_1;
reg signed [7:0] pix_2;
reg signed [7:0] pix_3;
reg signed [7:0] pix_4;
reg signed [7:0] pix_5;
reg signed [7:0] pix_6;
reg signed [7:0] pix_7;
reg signed [7:0] pix_8;
reg signed [7:0] pix_9;
reg signed [7:0] pix_10;
reg signed [7:0] pix_11;
reg signed [7:0] pix_12;
reg signed [7:0] pix_13;
reg signed [7:0] pix_14;
reg signed [7:0] pix_15;
reg signed [7:0] pix_16;
reg signed [7:0] pix_17;
reg signed [7:0] pix_18;
reg signed [7:0] pix_19;
reg signed [7:0] pix_20;
reg signed [7:0] pix_21;
reg signed [7:0] pix_22;
reg signed [7:0] pix_23;
reg signed [7:0] pix_24;
wire signed [30:0] pix_o_x;
wire signed [30:0] pix_o_y;

sobel_conv_x u0_sobel_conv_x
	(
		.pix_0		(pix_0	),
		.pix_1		(pix_1	),
		.pix_2		(pix_2	),
		.pix_3		(pix_3	),
		.pix_4		(pix_4	),
		.pix_5		(pix_5	),
		.pix_6		(pix_6	),
		.pix_7		(pix_7	),
		.pix_8		(pix_8	),
		.pix_9		(pix_9	),
		.pix_10		(pix_10	),
		.pix_11		(pix_11	),
		.pix_12		(pix_12	),
		.pix_13		(pix_13	),
		.pix_14		(pix_14	),
		.pix_15		(pix_15	),
		.pix_16		(pix_16	),
		.pix_17		(pix_17	),
		.pix_18		(pix_18	),
		.pix_19		(pix_19	),
		.pix_20		(pix_20	),
		.pix_21		(pix_21	),
		.pix_22		(pix_22	),
		.pix_23		(pix_23	),
		.pix_24		(pix_24	),
		.pix_o		(pix_o_x)
	);

sobel_conv_y u0_sobel_conv_y
	(
		.pix_0		(pix_0	),
		.pix_1		(pix_1	),
		.pix_2		(pix_2	),
		.pix_3		(pix_3	),
		.pix_4		(pix_4	),
		.pix_5		(pix_5	),
		.pix_6		(pix_6	),
		.pix_7		(pix_7	),
		.pix_8		(pix_8	),
		.pix_9		(pix_9	),
		.pix_10		(pix_10	),
		.pix_11		(pix_11	),
		.pix_12		(pix_12	),
		.pix_13		(pix_13	),
		.pix_14		(pix_14	),
		.pix_15		(pix_15	),
		.pix_16		(pix_16	),
		.pix_17		(pix_17	),
		.pix_18		(pix_18	),
		.pix_19		(pix_19	),
		.pix_20		(pix_20	),
		.pix_21		(pix_21	),
		.pix_22		(pix_22	),
		.pix_23		(pix_23	),
		.pix_24		(pix_24	),
		.pix_o		(pix_o_y)
	);


localparam	length = 19'd262144;
localparam	length_1 = 19'd331704;
reg [7:0] pix_data [0:length-1];
reg [7:0] pix_data_1 [0:length_1-1];
integer i,j;


initial	begin
	i = 0;
	$readmemb("D:\\Document\\Code\\Python\\image-filtering\\image-data\\Peppers_gray.txt",pix_data);
	for (i=0;i<=length_1-1;i=i+1) begin
	    if ((516*2+1<i)&&(i<516*513-2)&&(2<i%516)&&(i%516<514)) begin
	    	pix_data_1[i] = pix_data[(i/516 - 2)*512 + (i%516) - 2];
	    end
	    else begin
	    	pix_data_1[i] = 8'd0;
	    end
	    //$display("pix_data_1[%d]: %b", i, pix_data_1[i]);
	end
end

reg clk;
always #5 clk = ~clk;


initial	begin
	j = 0;
	clk = 0;
	pix_0 = 0;
	pix_1 = 0;
	pix_2 = 0;
	pix_3 = 0;
	pix_4 = 0;
	pix_5 = 0;
	pix_6 = 0;
	pix_7 = 0;
	pix_8 = 0;
	pix_9 = 0;
	pix_10 = 0;
	pix_11 = 0;
	pix_12 = 0;
	pix_13 = 0;
	pix_14 = 0;
	pix_15 = 0;
	pix_16 = 0;
	pix_17 = 0;
	pix_18 = 0;
	pix_19 = 0;
	pix_20 = 0;
	pix_21 = 0;
	pix_22 = 0;
	pix_23 = 0;
	pix_24 = 0;
	#100
	for (j=0;j<=length-1;j=j+1) begin
		@(posedge clk)
	 	pix_0 = pix_data_1[(j/512 + 0)*516 + (j%512) + 0];
		pix_1 = pix_data_1[(j/512 + 0)*516 + (j%512) + 1];
		pix_2 = pix_data_1[(j/512 + 0)*516 + (j%512) + 2];
		pix_3 = pix_data_1[(j/512 + 0)*516 + (j%512) + 3];
		pix_4 = pix_data_1[(j/512 + 0)*516 + (j%512) + 4];
		pix_5 = pix_data_1[(j/512 + 1)*516 + (j%512) + 0];
		pix_6 = pix_data_1[(j/512 + 1)*516 + (j%512) + 1];
		pix_7 = pix_data_1[(j/512 + 1)*516 + (j%512) + 2];
		pix_8 = pix_data_1[(j/512 + 1)*516 + (j%512) + 3];
		pix_9 = pix_data_1[(j/512 + 1)*516 + (j%512) + 4];
		pix_10 = pix_data_1[(j/512 + 2)*516 + (j%512) + 0];
		pix_11 = pix_data_1[(j/512 + 2)*516 + (j%512) + 1];
		pix_12 = pix_data_1[(j/512 + 2)*516 + (j%512) + 2];
		pix_13 = pix_data_1[(j/512 + 2)*516 + (j%512) + 3];
		pix_14 = pix_data_1[(j/512 + 2)*516 + (j%512) + 4];
		pix_15 = pix_data_1[(j/512 + 3)*516 + (j%512) + 0];
		pix_16 = pix_data_1[(j/512 + 3)*516 + (j%512) + 1];
		pix_17 = pix_data_1[(j/512 + 3)*516 + (j%512) + 2];
		pix_18 = pix_data_1[(j/512 + 3)*516 + (j%512) + 3];
		pix_19 = pix_data_1[(j/512 + 3)*516 + (j%512) + 4];
		pix_20 = pix_data_1[(j/512 + 4)*516 + (j%512) + 0];
		pix_21 = pix_data_1[(j/512 + 4)*516 + (j%512) + 1];
		pix_22 = pix_data_1[(j/512 + 4)*516 + (j%512) + 2];
		pix_23 = pix_data_1[(j/512 + 4)*516 + (j%512) + 3];
		pix_24 = pix_data_1[(j/512 + 4)*516 + (j%512) + 4];   
	end

end



integer file_handle_x, file_handle_y, k, l;
initial	begin
	fork
		begin
			file_handle_x = $fopen("D:\\Document\\Code\\Python\\image-filtering\\image-data\\Peppers_gray_edge_detect_x.txt", "w");
			#100
			for (k=0;k<=length-1;k=k+1) begin
			    @(posedge clk)
			    $fdisplay(file_handle_x, "%d", pix_o_x);
			    //$display("pix_o[%d] = %b", k, pix_o);
			end
			$fclose(file_handle_x);
		end
		begin
			file_handle_y = $fopen("D:\\Document\\Code\\Python\\image-filtering\\image-data\\Peppers_gray_edge_detect_y.txt", "w");
			#100
			for (l=0;l<=length-1;l=l+1) begin
			    @(posedge clk)
			    $fdisplay(file_handle_y, "%d", pix_o_y);
			    //$display("pix_o[%d] = %b", k, pix_o);
			end
			$fclose(file_handle_y);			
		end
	join
	$display("---------------------------------------");
	$display("\t\tSimulation finsh");
	$display("---------------------------------------");
	#1
	$finish;
end






endmodule